package com.cyl.manager.ums.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysDictType;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysDictTypeService;

import io.swagger.annotations.Api;
/**
 * 会员信息Controller
 * 
 * @author zcc
 * @date 2022-11-28
 */
@Api(description ="会员标签接口列表")
@RestController
@RequestMapping("/ums/memberTag")
public class MemberTagController extends BaseController {
	public static final String MEMBER_TAG_ZBJ  = "MEMBER_TAG_ZBJ_";

    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private ISysDictTypeService dictTypeService;

    @PreAuthorize("@ss.hasPermi('member:dict:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysDictData dictData)
    {
    	  Long qrDeptZbjId = SecurityUtils.getDeptZbjId();
          if(qrDeptZbjId==null) {
        	  return getDataTableError("直播间账号才能操作！");
          }
        dictData.setDictType(getDictType());
        startPage();
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        return getDataTable(list);
    }
    
    /**
     * 新增字典类型
     */
   
    public void addSysDictType(SysDictType dict)
    {
        if (UserConstants.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dict)))
        {
            return ;
        }
        dict.setCreateBy(getUserId());
        dictTypeService.insertDictType(dict);
        return;
    }
    
    
    private String getDictType() {
    	Long qrDeptZbjId = SecurityUtils.getDeptZbjId();
    	String dictType = MEMBER_TAG_ZBJ+qrDeptZbjId;
    	SysDictType sysDictType = new SysDictType();
    	sysDictType.setDictType(dictType);
    	sysDictType.setDictName("直播间"+qrDeptZbjId+"会员标签");
    	addSysDictType(sysDictType);
    	return dictType;
    }

    /**
     * 查询字典数据详细
     */
    @PreAuthorize("@ss.hasPermi('member:dict:query')")
    @GetMapping(value = "/{dictCode}")
    public AjaxResult getInfo(@PathVariable Long dictCode)
    {
        return AjaxResult.success(dictDataService.selectDictDataById(dictCode));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/typeTag")
    public AjaxResult dictType()
    {
    	
    	Long qrDeptZbjId = SecurityUtils.getDeptZbjId();
    	String dictType = MEMBER_TAG_ZBJ+qrDeptZbjId;
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (StringUtils.isNull(data))
        {
            data = new ArrayList<SysDictData>();
        }
        return AjaxResult.success(data);
    }
 
    /**
     * 新增字典类型
     */
    @PreAuthorize("@ss.hasPermi('member:dict:add')")
    @Log(title = "会员标签数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysDictData dict)
    {
    	 Long qrDeptZbjId = SecurityUtils.getDeptZbjId();
         if(qrDeptZbjId==null) {
       	  	return error("直播间账号才能操作此功能！");
         }
         dict.setDictType(getDictType());
        dict.setCreateBy(getUserId());
        int num =  dictDataService.insertDictData(dict);
        dictTypeService.resetDictCache();

        return toAjax(num);
    }

    /**
     * 修改保存字典类型
     */
    @PreAuthorize("@ss.hasPermi('member:dict:edit')")
    @Log(title = "会员标签数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysDictData dict)
    {
    	Long qrDeptZbjId = SecurityUtils.getDeptZbjId();
        if(qrDeptZbjId==null) {
      	  	return error("直播间账号才能操作此功能！");
        }
        dict.setDictType(getDictType());
        dict.setUpdateBy(getUserId());
        int num = dictDataService.updateDictData(dict);
        dictTypeService.resetDictCache();
        return toAjax(num);
    }

    /**
     * 删除字典类型
     */
    @PreAuthorize("@ss.hasPermi('member:dict:remove')")
    @Log(title = "会员标签类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictCodes}")
    public AjaxResult remove(@PathVariable Long[] dictCodes)
    {
        dictDataService.deleteDictDataByIds(dictCodes);
        dictTypeService.resetDictCache();
        return success();
    }
    
}
